﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.Collections.Generic;

namespace Tesla.Graphics {
    /// <summary>
    /// Sampler State collection for materials, simply a container that mirrors the number of valid samplers
    /// the renderer allows.
    /// </summary>
    public sealed class MaterialSamplerStateCollection : ISamplerStateCollection {

        private SamplerState[] _samplers;
        private bool _isVertex;

        /// <summary>
        /// Gets or sets the <see cref="Tesla.Graphics.SamplerState"/> at the specified index.
        /// </summary>
        public SamplerState this[int index] {
            get {
                if(index < 0 || index >= _samplers.Length) {
                    throw new ArgumentOutOfRangeException("Invalid sampler index.");
                }
                return _samplers[index];
            }
            set {
                if(index < 0 || index >= _samplers.Length) {
                    throw new ArgumentOutOfRangeException("Invalid sampler index.");
                }
                _samplers[index] = value;
            }
        }

        /// <summary>
        /// Gets the maximum number of sampler states that can be set.
        /// </summary>
        public int MaxSamplers {
            get {
                return _samplers.Length;
            }
        }

        internal MaterialSamplerStateCollection(int maxSamplers, bool isVertex) {
            _samplers = new SamplerState[maxSamplers];
            _isVertex = isVertex;
        }

        /// <summary>
        /// Applies the render states.
        /// </summary>
        /// <param name="renderer">The renderer.</param>
        public void ApplyRenderStates(IRenderer renderer) {
            for(int i = 0; i < _samplers.Length; i++) {
                SamplerState ss = _samplers[i];
                if(ss != null) {
                    if(_isVertex) {
                        renderer.VertexSamplerStates[i] = ss;
                    } else {
                        renderer.SamplerStates[i] = ss;
                    }
                }
            }
        }

        /// <summary>
        /// Clears this collection.
        /// </summary>
        public void Clear() {
            Array.Clear(_samplers, 0, _samplers.Length);
        }

        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns></returns>
        public MaterialSamplerStateCollection Clone() {
            MaterialSamplerStateCollection ssc = new MaterialSamplerStateCollection(_samplers.Length, _isVertex);
            Array.Copy(_samplers, ssc._samplers, _samplers.Length);
            return ssc;
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<SamplerState> GetEnumerator() {
            return (IEnumerator<SamplerState>) _samplers.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return _samplers.GetEnumerator();
        }

    }
}
